package top.hidoctor.auscultation.widget.tree;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * TREE VIEW NODE
 * Created by wudeng on 2017/3/29.
 */

public class TreeNode2 {

    private int type; //0 单选 1 多选
    private String id;
    private String pId;
    private String title;
    private int level;
    private boolean isExpand = true; //默认全部展开
    private boolean checked = false;
    private List<TreeNode2> children = new ArrayList<>();
    private TreeNode2 parent;
    private Object value;
    private RViewHolder mViewHolder;

    public TreeNode2(String id, String pId, String title, Integer type, Object value) {
        super();
        this.id = id;
        this.pId = pId;
        this.title = title;
        this.type = type;
        this.value = value;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return pId;
    }

    public void setPid(String pId) {
        this.pId = pId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public boolean isExpand() {
        return isExpand;
    }

    public void setExpand(boolean isExpand) {
        this.isExpand = isExpand;
        if (!isExpand) {
            for (TreeNode2 node : children) {
                node.setExpand(false);
            }
        }
    }

    public boolean isRoot() {
        return parent == null;
    }

    public boolean isLeaf() {
        return children.size() == 0;
    }

    public List<TreeNode2> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode2> children) {
        this.children = children;
    }

    public TreeNode2 getParent() {
        return parent;
    }

    public void setParent(TreeNode2 parent) {
        this.parent = parent;
    }

    public boolean isParentExpand() {
        return parent != null && parent.isExpand();
    }

    public Object getValue() {
        return value;
    }

    public RViewHolder getViewHolder() {
        return mViewHolder;
    }

    public void setViewHolder(RViewHolder viewHolder) {
        mViewHolder = viewHolder;
    }

    public boolean isChecked() {
        return checked;
    }

    /**
     * @param checked     是否选择
     * @param updateChild 是否有child 以及更新
     */
    public void setChecked(boolean checked, boolean updateChild) {
        Log.e("TAG", "setChecked: " + getType());
        if (type == 0) {//单选
            resetState();
        }
        this.checked = checked;
        updateParentState();
        if (updateChild) {
            updateChildState(checked);
        }

    }

    /**
     * 重置选择
     */
    private void resetState() {
        for (TreeNode2 node : parent.children) {
            node.setChecked(false, true);
        }
    }

    private void updateChildState(boolean checked) {
        // 更新叶节点
        for (TreeNode2 node : children) {
            node.setChecked(checked, true);
        }
    }

    /**
     * 设置父节点
     */
    private void updateParentState() {
        if (parent != null) {
            boolean allCheck = true;
            for (TreeNode2 node : parent.children) {
                allCheck = allCheck && node.checked;
            }
//            if (allCheck) {
//                parent.setChecked(true, false);
//            } else {
//                parent.setChecked(false, false);
//            }
        }
    }
}
